﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace BodBody {

    public class Player1 {

        private Game game;

        public Texture2D text2D { get; set; }


        List<Texture2D> list = new List<Texture2D>();

        Dictionary<string, Texture2D> dict = new Dictionary<string, Texture2D>();

        private string Index = "Player/sta0";

        private int currentElapsed;

        private int secondsPerFrame = 60;

        private int intIndex = 0;

        private string action = "Player/sta{0}";

        private int fangxiang = 0;

        public Player1(Game game) {
            init(game);
        }

        public void init(Game game) {
            this.game = game;
            for (int i = 0; i < 15; i++) {
                var run = string.Format("Player/run{0}", i);
                var sta = string.Format("Player/sta{0}", i);
                dict.Add(run, this.game.Content.Load<Texture2D>(run));
                dict.Add(sta, this.game.Content.Load<Texture2D>(sta));
            }

        }

        public void Update(GameTime gameTime) {
            var keyState = Keyboard.GetState();
            if (keyState.IsKeyDown(Keys.Space)) {
                this.game.Exit();
            }
            action = "Player/sta{0}";
            if (keyState.IsKeyDown(Keys.D)) {
                fangxiang = 0;
                action = "Player/run{0}";
            }

            if (keyState.IsKeyDown(Keys.A)) {
                fangxiang = 1;
                action = "Player/run{0}";
            }

            currentElapsed = currentElapsed + gameTime.ElapsedGameTime.Milliseconds;
            if ((this.currentElapsed > this.secondsPerFrame)) {
                this.currentElapsed = 0;
                if (intIndex >= 15) {
                    intIndex = 0;
                }
                Index = string.Format(action, intIndex);
                intIndex++;
            }
        }

        public void Draw(SpriteBatch spriteBatch) {
            spriteBatch.Begin();
            spriteBatch.Draw(dict[Index], new Rectangle(game.Window.ClientBounds.Width / 2, game.Window.ClientBounds.Height / 3,
                dict[Index].Width, dict[Index].Height), null, Color.BurlyWood, 0, new Vector2(0, 0), (SpriteEffects)fangxiang, 0);
            spriteBatch.End();
        }
    }
}
